<!DOCTYPE html>
<html>

<head lang="en">
    <title>找攻略</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap-4.1.1-dist/css/bootstrap.min.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/strategy.css"/>
    <script src="/js/plugins/jrender/jrender.min.js"></script>

    <link rel="stylesheet" href="/js/plugins/dialog2/dialog.css">
    <script src="/js/plugins/dialog2/dialog.min.js"></script>

    <script>
        $(function () {
            //默认加载攻略推荐游记 和 当季推荐攻略

            //请求获取攻略推荐游记
            $.get('/travelComments', {type: 3, pageSize: 1}, function (data) {
                //渲染数据  list只有一个
                $('.commend').renderValues(data.list[0], {
                    getHref: function (item, value) {
                        $(item).attr('href', $(item).data('href') + value)
                    }
                });
            });

            //获取当季推荐攻略  参数 state=1 --> 推荐状态  pageSize=0 不分页
            $.get('/strategies', {state: 1, pageSize: 0}, function (data) {
                $('.strategyCommend').renderValues(data, {
                    getHref: function (item, value) {
                        $(item).attr('href', $(item).data('href') + value)
                    }
                });
            });

            var regionId;
            //获取所有推荐的地区 状态为1 data 为list
            $.get('/regions',{state:1},function (data) {
                //绑定数据在导航栏上--->> li 与 div
                $.each(data,function (index, ele) {
                    var li='<li class="nav-item"><a data-id="'+ ele.id+'" class="nav-link" data-toggle="pill" href="#pills-'+ele.id+'">'+ele.name+'</a></li>'
                    var div='<div class="tab-pane fade" id="pills-'+ ele.id+'"></div>';
                    $('#pills-tab').append(li);
                    $('#pills-tabContent').append(div);
                });
                
                //绑定点击事件
                $('.nav-link').click(function () {
                    //获取地区的id
                    regionId = $(this).data('id');
                    console.log(regionId);
                    if(regionId){
                        //发请求查询该地区的推荐攻略 及 全部攻略
                        $.get('/regions/'+regionId+'/strategies',function (data) {
                            $('#pills-'+regionId).html(data);
                        },'html')
                    }
                });
            });

            //声明一个变量,用于当前页的增加
            var currentPage = 2;
            //总页数
            var pages=2;
            //存储游记的数组

            //定义一个函数,函数里发请求,使得变量的值金有限于该次访问
            function query() {
                if(regionId){
                    $.get('/regions/'+regionId+'/strategies', {currentPage: currentPage}, function (data) {
                        console.log(data);
                        $.each(data.list,function (index, ele) {
                            //对每个list 利用模板克隆
                            var temp = $('#template .mb').clone(true);
                            temp.find('a').attr('href','/strategyCatalogs.html?id='+ele.id);
                            temp.find('img').attr('src',ele.coverUrl);
                            temp.find('span').html(ele.title);
                            //拼接
                            $('.classify').append(temp)
                        });
                        //获取总页数
                        pages = data.pages;
                    });
                    //当前页加1
                    currentPage = currentPage + 1;
                    }
            };

            //滚动事件
            $(window).scroll(function () {
                if ($(window).height() + $(document).scrollTop() + 1 >= $(document).height()) {
                    if (currentPage <= pages) {
                        query();
                    } else {
                        $(document).dialog({
                            type: 'notice',
                            content: '<span class="info-text">已经到底了</span>',
                            autoClose: 2000
                        })
                    }
                }
            })


        })

    </script>

</head>

<body>

<div id="template" style="display: none">
    <div class="col-6 mb">
        <a>
            <img class="float-left">
            <div class="classify-text">
                <span></span>
                <p>0人收藏</p>
            </div>
        </a>
    </div>
</div>

<div class="search-head">
    <div class="row nav-search">
        <div class="col-2">
            <a href="index.html">
                <span><i class="fa fa-chevron-left fa-2x"></i></span>
            </a>
        </div>
        <div class="col-10">
            <div class="input-group-sm search">
                <input class="form-control searchBtn" placeholder="找攻略">
            </div>
        </div>
    </div>

</div>

<ul class="nav nav-pills nav-justified border border-left-0 border-top-0 border-right-0" id="pills-tab" role="tablist">
    <li class="nav-item">
        <a class="nav-link active show" data-toggle="pill" href="#pills-top">
            <span style="font-weight: bold;">推荐</span>
        </a>
    </li>
</ul>

<div class="tab-content" id="pills-tabContent">
    <div class="tab-pane fade active show" id="pills-top">
        <div class="container commend">
            <a data-href="travelContent.html?id=" render-fun="getHref" render-key="travelId">
                <img render-src="coverUrl">
                <p render-html="title"></p>
            </a>
        </div>
        <hr>
        <div class="container strategyCommend">
            <h6>当季推荐</h6>
            <div class="row hot" render-loop="list">
                <div class="col-4">
                    <a data-href="strategyCatalogs.html?id=" render-fun="getHref" render-key="list.id">
                        <img render-src="list.coverUrl">
                        <p render-html="list.title"></p>
                    </a>
                </div>
            </div>
        </div>
    </div>

</div>


</body>

</html>